/**
 * 
 */
package cc.telecomdigital.tdstock.activity.groups.more.push.util;

import java.util.HashMap;

import org.json.JSONException;
import org.json.JSONObject;

import cc.telecomdigital.tdstock.CommonDefn;
import cc.telecomdigital.tdstock.ITDLApplication;
import cc.telecomdigital.tdstock.SystemInfo;
import cc.telecomdigital.tdstock.Framework.UniqueItem.DeviceInfo;
import cc.telecomdigital.tdstock.Framework.UniqueItem.TDStockLog;
import cc.telecomdigital.tdstock.Services.LS_ClientParamMemory;
import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;

/**
 * 
 * Project：push Package：cc.telecomdigital.tdstock.activity.groups.more.push.util
 * Class：Data Description： Creater：hunter CreateTime：Oct 10, 2011 5:52:23 PM
 * Modifier ：hunter Modify time：Oct 10, 2011 5:52:23 PM Modify Remarks：
 * 
 * @version 1.0
 * 
 */
public class Data
{
	/* message list url */
	// 进入list UI 不论程序是否启动
	private static String	_message_list_url				= "http://%s/notification/getNotifyMessageHistory.do?account=%s&token=%s&device_token=%s&device_type=2&view=gphone&version=1.0";

	/* message content url */
	// 推送消息时，推送参数包含message_id
	private static String	_message_push_content_url		= "http://%s/notification/getNotifyMessageHistory.do?account=%s&token=%s&message_type_id=%s&message_id=%s";
	// 正常登录，选中list的单一选项进入到详细UI
	// private static String
	// _message_click_content_url="http://%s/notification/stkNewsDetail.do?account=%s&token=%s&message_type_id=%s&id=%s";

	// private static String
	// _push_controller_host_url="http://192.168.199.52:8080/androidPushController/queryhost.do?device_token=%s";
	private static String	_push_controller_host_url		= "http://%s/androidPushController/queryhost.do?device_token=%s";

	private static String	_update_push_server_profile_url	= "http://#webhost#/notification/updateAccountProfile.do?account=#account#&token=#token#&device_type=#devicetype#&#deviceidparam#=#deviceid#&#hostparam#=#pushserverhost#";

	private static String	_stock_new_version_news			= "http://%s/iphone/getMarketInfo.do?account=%s&token=%s&stockcode=%s";

	private static String	_price_alert_url				= "http://#webhost#/notification/setPriceAlert.do?account=#account#&token=#token#";

	// 域名
	public static String	webServer						= LS_ClientParamMemory
															.GetWebServerInfo();
	/*
	 * public static String URL = "http://%s/" +
	 * "notification/internal/getAccountProfile.do?" + "nsid=" + "&account=%s";
	 */

	public static String	notification_profile			= "http://%s/"
															+ "notification/getAccountProfile.do?"
															+ "nsid="
															+ "&account=%s&token=%s";
	// this is message
	public static String	id							= "";																																		// 消息ID
	/*
	 * public static String dateTime=""; //消息时间 public static String status="";
	 * //装态 public static String vendor="D"; public static String title=""; //标题
	 * public static String content=""; //详细内容
	 */
	// this is message list
	public static String	message_type_id				= "2";
	public static String	message_list_html				= "";
	public static String	device_token					= "";

	// this is account and token
	public static String	account						= "";
	public static String	token						= "";

	/*   *//**
			* 单击消息列表获取消息URL
			* 
			* @param account
			* @param id
			* @return
			*/
	/*
	 * public static String getMessageClickContentURL(Context context,String
	 * id){ getDomainAndAccountAndToken(context); String
	 * url=String.format(Data._message_click_content_url,
	 * Data._net_url,Data.account,Data.token,Data.message_type_id,id); return
	 * url; }
	 */
	/**
	 * 推送消息URL
	 * 
	 * @param context
	 * @param id
	 * @return
	 */
	public static String getMessagePushContentURL(Context context,
			String messagetypeid, String id)
	{
		getDomainAndAccountAndTokenAndDevice_token(context);
		String url = String
				.format(Data._message_push_content_url, Data.webServer,
						Data.account, Data.token, messagetypeid, id);
		return url;
	}

	/**
	 * 新版news URL
	 * 
	 * @param context
	 * @param stock
	 * @return
	 */
	public static String getNewVersionNewsContentURL(Context context,
			String stockCode)
	{
		getDomainAndAccountAndTokenAndDevice_token(context);
		String url = String.format(Data._stock_new_version_news,
				Data.webServer, Data.account, Data.token, stockCode);
		return url;
	}

	/**
	 * 更新push server URL
	 * 
	 * @param context
	 * @param host
	 *            , i.e. (androidpush.telecomdigital.cc)
	 * @return
	 */
	public static String getUpdatePushServerProfileURL(Context context,
			String host)
	{
		getDomainAndAccountAndTokenAndDevice_token(context);
		// String url=String.format(Data._stock_new_version_news,
		// Data.webServer,Data.account,Data.token,stockCode);

		String url = _update_push_server_profile_url
				.replace("#webhost#", Data.webServer)
				.replace("#account#", Data.account)
				.replace("#token#", Data.token)
				.replace("#devicetype#",
						String.valueOf(DeviceInfo.GetDeviceTypeID()))
				.replace("#deviceidparam#", "device_token")
				.replace("#deviceid#", Data.device_token)
				.replace("#hostparam#", "androidPushServer")
				.replace("#pushserverhost#", host);

		return url;
	}

	/**
	 * 获取消息列表url
	 * 
	 * @param content
	 * @return
	 */
	public static String getMessageListURL(Context context)
	{
		getDomainAndAccountAndTokenAndDevice_token(context);
		Data.device_token = SystemInfo.getDeviceId(context);
		String url = String.format(Data._message_list_url, Data.webServer,
				Data.account, Data.token, Data.device_token);
		return url;
	}

	public static void getDomainAndAccountAndTokenAndDevice_token(
			Context context)
	{
		ITDLApplication application = (ITDLApplication) context
				.getApplicationContext();
		Data.account = application.getSessionAccount();
		Data.token = application.getSessionToken();
		Data.webServer = /* application.getWebServer(); */LS_ClientParamMemory
				.GetWebServerInfo();
		Data.device_token = SystemInfo.getDeviceId(context);

		if (Data.account == null || "".equals(Data.account))
		{
			SharedPreferences _prefs = context.getSharedPreferences(
					CommonDefn.SYSTEM_CURRENT_SETTING,
					Context.MODE_PRIVATE);
			Data.account = _prefs.getString(
					CommonDefn.PREFERENCE_LAST_ACCOUNT_NAME, "");
		}
	}

	//
	public static String getPushControllerURL(String key, Context context)
	{
		getDomainAndAccountAndTokenAndDevice_token(context);
		return String.format(_push_controller_host_url, Data.webServer, key);
	}

	/*
	 * public static HashMap<String,String> getHostDomainAndPort(String
	 * tag,String newUserName){ HashMap<String,String> maps=null; String
	 * url=Data.getHostInfoURL(newUserName); Log.d(tag, "fatch...."); String
	 * content=DataRead.getContentFromURL(url);
	 * if(null!=content&&!"".equals(content)){ JSONObject jsonObject; try {
	 * jsonObject = new JSONObject(content); } catch (JSONException e1) { //
	 * TODO Auto-generated catch block return null; } try { String
	 * key=jsonObject.getString("device_token"); String
	 * domain=jsonObject.getString("domain"); String
	 * port=jsonObject.getString("port"); if(key!=null&&!"".equals(key)){
	 * maps=new HashMap<String, String>(); maps.put("device_token", key);
	 * maps.put("domain", domain); maps.put("port", port); return maps; }else{
	 * return null; }
	 * 
	 * 
	 * 
	 * String status=jsonObject.getString("status");
	 * if(status!=null&&!"".equals(status)&&"exist".equals(status)){ maps=new
	 * HashMap<String, String>(); maps.put("status", "exist"); return maps;
	 * }else if(status==null){ return null; }else if("".equals(status)){ String
	 * key=jsonObject.getString("device_token"); String
	 * domain=jsonObject.getString("domain"); String
	 * port=jsonObject.getString("port"); if(key!=null&&!"".equals(key)){
	 * maps=new HashMap<String, String>(); maps.put("device_token", key);
	 * maps.put("domain", domain); maps.put("port", port); return maps; }else{
	 * return null; } }else{ return null; }
	 * 
	 * 
	 * 
	 * } catch (JSONException e) { // TODO Auto-generated catch block String key
	 * = null; String domain = null; String port = null; try { key =
	 * jsonObject.getString("device_token");
	 * domain=jsonObject.getString("domain"); port=jsonObject.getString("port");
	 * } catch (JSONException e1) { // TODO Auto-generated catch block }
	 * if(domain!=null&&!"".equals(domain)){ maps=new HashMap<String, String>();
	 * maps.put("device_token", key); maps.put("domain", domain);
	 * maps.put("port", port); return maps; }else{ return null; } } }else{
	 * return null; } }
	 */

	public static HashMap<String, String> getHostDomainAndPort(String tag,
			String newUserName, Context context)
	{
		HashMap<String, String> maps = null;
		String url = Data.getPushControllerURL(newUserName, context);
		TDStockLog.d(tag, "fatch....Url=" + (url == null ? "" : url));
		String content = DataRead.getContentFromURL(url);
		TDStockLog.d(tag, content == null ? "content==null" : content);
		if (null != content && !"".equals(content))
		{
			JSONObject jsonObject;
			try
			{
				jsonObject = new JSONObject(content);
			}
			catch (JSONException e1)
			{
				// TODO Auto-generated catch block
				return null;
			}
			try
			{
				String key = jsonObject.getString("device_token");
				String domain = jsonObject.getString("domain");
				String port = jsonObject.getString("port");
				if (domain != null && !"".equals(domain))
				{
					maps = new HashMap<String, String>();
					maps.put("device_token", key);
					maps.put("domain", domain);
					maps.put("port", port);
					return maps;
				}
				else
				{
					return null;
				}
			}
			catch (JSONException e)
			{
				// TODO Auto-generated catch block
				return null;
			}
		}
		else
		{
			return null;
		}
	}

	public static String[] verify(String tag, String newUsername,
			Context context)
	{
		HashMap<String, String> maps = getHostDomainAndPort(tag, newUsername,
				context);
		if (maps == null)
		{
			return null;
		}
		String host = null;
		String portString = null;
		if (maps != null && maps.size() == 1)
		{
			return new String[] { "exist" };
		}
		if (maps != null && maps.size() > 1)
		{
			host = maps.get("domain");
			portString = maps.get("port");
		}
		int port;
		try
		{
			port = Integer.parseInt(portString.trim());
		}
		catch (Exception e)
		{
			port = -1;
		}

		if (host != null && !"".equals(host) && port != -1)
		{
			return new String[] { host, portString };
		}
		else
		{
			return null;
		}
	}

	public static String getNotificationSettingURL(Context context)
	{
		// return String.format(URL, webServer
		// ,((ITDLApplication)context.getApplicationContext()).getSessionAccount());
		getDomainAndAccountAndTokenAndDevice_token(context);
		return String.format(notification_profile, Data.webServer,
				Data.account, Data.token);
	}

	public static String getPriceAlertURL(Context context)
	{
		getDomainAndAccountAndTokenAndDevice_token(context);
		String url = _price_alert_url.replace("#webhost#", /* Data.webServer */
		"quotedev2.telecomdigital.cc" /* 测试使用 */)
				.replace("#account#", Data.account)
				.replace("#token#", Data.token);
		return url;
	}
}
